package com.googlecode.spruce.web.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.googlecode.spruce.dao.ServiceGroupDao;
import com.googlecode.spruce.domain.ServiceGroup;

@Controller
@RequestMapping("/admin/servicegroup")
public class ServiceGroupDashboard
{
    @Autowired
    private ServiceGroupDao serviceGroupDao;

    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public ModelAndView getAll()
    {
        final ModelAndView model = new ModelAndView("servicegroup/index");

        final List<ServiceGroup> serviceGroups = this.serviceGroupDao.getAll();

        model.addObject("serviceGroups", serviceGroups);

        return model;
    }

    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public ModelAndView add(@RequestParam("name") String name)
    {
        final ServiceGroup serviceGroup = new ServiceGroup();
        serviceGroup.setName(name);

        this.serviceGroupDao.save(serviceGroup);

        return getAll();
    }
}
